package com.auxgroup.hr.front.service.personnal.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auxgroup.hr.front.dao.domain.PsJob;
import com.auxgroup.hr.front.service.personnal.JobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.dao.domain.PsCPersonalInfoClass;
import com.auxgroup.hr.front.dao.domain.PsCWorkhisTbl;
import com.auxgroup.hr.front.dao.repository.PsCWorkhisTblRepository;
import com.auxgroup.hr.front.service.comm.SysDictService;
import com.auxgroup.hr.front.service.personnal.WorkhisService;

/** 
 * @author lvliuhao
 * @version 创建时间：180526 
 * 工作历史信息service
 */
@Service("workhisService")
public class WorkhisServiceImpl implements WorkhisService {
	private static final Logger logger = LoggerFactory.getLogger(WorkhisServiceImpl.class);
	
	@Autowired
	private SysDictService sysDictService;
	
	@Autowired
	private PsCWorkhisTblRepository psCWorkhisTblRepository;
	@Autowired
	private JobService jobService;
	
	@Override
	public PsCWorkhisTbl getWorkhis(PsCPersonalInfoClass personalInfoClass) {
		return psCWorkhisTblRepository.findOne(personalInfoClass);
	}


	@Override
	public PsCWorkhisTbl save(PsCWorkhisTbl Workhis) {
		return psCWorkhisTblRepository.save(Workhis);
	}


	@Override
	public Boolean delete(PsCPersonalInfoClass personalInfoClass) {
		try {
			psCWorkhisTblRepository.delete(personalInfoClass);
		} catch (Exception e) {
			logger.error("delete Workhis error: personalInfoClass={}",personalInfoClass,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsCWorkhisTbl> getAll(String emplid) {
		PsCWorkhisTbl Workhis = new PsCWorkhisTbl();
		Workhis.setEmplid(emplid);


		List<PsJob> psjobs = jobService.findAll(emplid);
		return psCWorkhisTblRepository.findAll(Example.of(Workhis),new Sort(Sort.Direction.DESC,"startDt"));


	}

	/* (non-Javadoc)
	 * @see ModWorkHiService#getNowWorkHis(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getNowWorkHis(String emplid) {
		List<Map<String, Object>> results = new ArrayList<Map<String,Object>>();
		Map<String, Object> result;
		List<Object[]> nowWorks = sysDictService.getObjectListFromSql(Constant.NOW_WORK_HIS_QUERY_SQL, new String[]{emplid});
		if(nowWorks!=null&&!nowWorks.isEmpty()){
			for (Object[] objects : nowWorks) {
//				"T.EMPLID, T.EMPL_RCD , T.EFFSEQ, T.BGN_DT, T.END_DT " +
//						" , T.RS_FULL_NAME , T.C_DESCR, T.C_DESCR1, T.C_DESCR2, T.RS_JOB_LEVEL "
				result = new HashMap<String, Object>();
				result.put("emplid", objects[0]);//员工ID
				result.put("emplRcd", objects[1]);//员工记录
				result.put("effseq", objects[2]);//生效序列
				result.put("bgnDt", objects[3]);//开始日期
				result.put("endDt", objects[4]);//结束日期
				result.put("cDescr", objects[6]);//部门
				result.put("cDescr1", objects[7]);//岗位
				result.put("cDescr2", objects[8]);//直接上级
				result.put("rsJobLevel", objects[9]);//职等
				result.put("cJobLevelP", objects[10]);//通用晋升职等
				result.put("cJobtypeDesc", objects[11]);//职务类型
				results.add(result);
			}
		}
		return results;
	}
	
}
